package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.VaultInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface.OnPassBackListener;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TipPopupView;
import com.haotunet.app.goldtreasure.views.TitleView;

public class MyVaultActivity extends BaseActivity implements OnClickListener {
	private TitleView mTitleView;
	private ImageView btnCheckDetail;
	private LinearLayout mFrozenLayout;
	private ImageView mFrozenTip;
	private RelativeLayout rlRegularGold;
	private RelativeLayout mCurrentProfitLayout;
	private TextView mTotalGrams, mTotalPrice, mCurrentGrams, mCurrentPandL, mCurrentProfit, mRegularGrams,
			mRegularPandL, mRegularProfit, mFrozenGold;
	private LinearLayout mTipLayout;
	private final int GET_VAULTPAGE_INFO_SUCCESS = 2;
	private Handler handle = new Handler() {

		@SuppressWarnings("deprecation")
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case GET_VAULTPAGE_INFO_SUCCESS:
					VaultInfo info = (VaultInfo) msg.obj;
					mTotalGrams.setText(info.getTotalGold());
					mTotalPrice.setText(info.getTotalAssets());
					mCurrentGrams.setText(info.getCurrentGold());
					if (Double.parseDouble(info.getFrozenGold()) != 0) {
						mFrozenLayout.setVisibility(View.VISIBLE);
						mFrozenGold.setText("冻结黄金为" + info.getFrozenGold() + "克");
					}
					if ("+".equals(info.getCurrentProfitLoss().trim().substring(0, 1))) {
						mCurrentPandL.setTextColor(getResources().getColor(R.color.color_red));
					} else if ("-".equals(info.getCurrentProfitLoss().trim().substring(0, 1))) {
						mCurrentPandL.setTextColor(getResources().getColor(R.color.color_green));
					} else {
						mCurrentPandL.setTextColor(getResources().getColor(R.color.color_black));
					}
					mCurrentPandL.setText(info.getCurrentProfitLoss());
					mCurrentProfit.setText(info.getCurrentEarnings());
					mRegularGrams.setText(info.getFixedPeriodGold());
					mRegularProfit.setText(info.getTodayFixedPeriodEarnings());
					mRegularPandL.setText(info.getFixedPeriodEarnings());
					break;
				default:
					break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_vault);
		initView();
		initData();
	}

	private void initData() {

		ActionUtil actionUtil = new ActionUtil(MyVaultActivity.this);
		actionUtil.getVaultPageInfo();
		actionUtil.setOnPassBackListener(new OnPassBackListener() {

			@Override
			public void passBack(BaseModel model) {
				if (isFinishing()){
					return;
				}
				VaultInfo vaultInfo = (VaultInfo) model;
				if (vaultInfo == null) {
					UIHelper.showToast(MyVaultActivity.this, getString(R.string.rank_fail));
					return;
				}
				if (vaultInfo.getErrorMsg() != null) {
					if (vaultInfo.getErrorCode().equals(Strs.EXTRUSION_SIGN_IN)) {
						ActivityUtils.forceQuit(MyVaultActivity.this, getString(R.string.crowding_in_tips_txt));
					} else {
						UIHelper.showToast(MyVaultActivity.this, vaultInfo.getErrorMsg());
					}
				} else {
					Message msg = new Message();
					msg.what = GET_VAULTPAGE_INFO_SUCCESS;
					msg.obj = vaultInfo;
					handle.sendMessage(msg);
				}
			}
		});
	}

	private void initView() {
		mTitleView = (TitleView) findViewById(R.id.title_view);
		mTitleView.getTitletextView().setVisibility(View.VISIBLE);
		mTitleView.getTitletextView().setText(R.string.title_my_vault);

		mTitleView.getBackView().setVisibility(View.VISIBLE);
		mTitleView.getBackView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mFrozenLayout = (LinearLayout) findViewById(R.id.frozen_gold_layout);
		mFrozenTip = (ImageView) findViewById(R.id.frozen_gold_tip);
		btnCheckDetail = (ImageView) findViewById(R.id.btn_check_detail);
		rlRegularGold = (RelativeLayout) findViewById(R.id.rl_regular_gold);
		mCurrentProfitLayout = (RelativeLayout) findViewById(R.id.rl_current_profit);
		mFrozenGold = (TextView) findViewById(R.id.txt_frozen_gold);
		mTotalGrams = (TextView) findViewById(R.id.total_gold_grams);
		mTotalPrice = (TextView) findViewById(R.id.total_price);
		mCurrentGrams = (TextView) findViewById(R.id.current_gold_grams);
		mCurrentPandL = (TextView) findViewById(R.id.current_profit_and_loss);
		mCurrentProfit = (TextView) findViewById(R.id.current_profit);
		mRegularGrams = (TextView) findViewById(R.id.regular_gold_grams);
		mRegularPandL = (TextView) findViewById(R.id.regular_profit_and_loss);
		mRegularProfit = (TextView) findViewById(R.id.regular_profit);
		mTipLayout = (LinearLayout) findViewById(R.id.tip_layout);
		mFrozenTip.setOnClickListener(this);
		mCurrentProfitLayout.setOnClickListener(this);
		btnCheckDetail.setOnClickListener(this);
		rlRegularGold.setOnClickListener(this);
		mTipLayout.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.frozen_gold_tip:
				new TipPopupView(MyVaultActivity.this,null, getString(R.string.frozen_gold_tip), R.style.MyDialog).show();
				break;
			case R.id.btn_check_detail:
				Intent intent = new Intent(MyVaultActivity.this, MyVaultDetailAct.class);
				startActivity(intent);
				break;
			case R.id.rl_regular_gold:
				ActivityUtils.gotoMyRegularProduct(MyVaultActivity.this);
				break;

			case R.id.rl_current_profit:
				ActivityUtils.gotoMyProfit(MyVaultActivity.this);
				break;
			case R.id.tip_layout:
				new TipPopupView(MyVaultActivity.this,null, getString(R.string.profit_tip), R.style.MyDialog).show();
				break;
			default:
				break;
		}
	}
}
